package ACoForest;

import java.util.ArrayList;
import java.util.HashMap;

import termWeighting.PrepareTF;
import type.TfType;
import util.BugListProcess;
import util.MapUtil;
import LPU.FirstStepForLPU;
import LPU.LPUInTwoSteps;
import dataProcess.BugInfoToTextFile;
import dataProcess.ReadExcel;
import ds.BugInfo;
import ds.Constants;

/**
 * train:labeled = positive + negative
 * test:unlabeled
 * rate = test / total
 * @author make
 *
 */
public class PrepareData {
	
	public ArrayList<BugInfo> allBugs;
	
	public PrepareData(){
		allBugs = new ArrayList<BugInfo>();
	}
	
	public void getBugs() {
		/**categories*/
		String[] originCategories = { "Functionality", "Security", "Usability", "Performance",
				"Security Defect", "UI", "Efficiency"};
		HashMap<String, String> mergeMap = new HashMap<String, String>();
		mergeMap.put("UI", "Usability");
		mergeMap.put("Security Defect", "Security");
		mergeMap.put("Efficiency", "Performance");
		/**text indexes in Excel file*/
		int[] textIndexes1 = {10};
		int[] textIndexes2 = {1, 8};
		int[] textIndexes4 = {3, 4};
		/**different data resource*/
		ReadExcel bugGetter = new ReadExcel(originCategories);
		allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports1.xls",  textIndexes1, 6));//MS 1
		allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports2.xls", textIndexes2, 4));//MS 2
		allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports4.xls", textIndexes4, 0));// Android 
		allBugs.addAll(bugGetter.getSecurityBugs("data/input/51security"));//Mozilla
		
		ArrayList<String> labelList = bugGetter.getLabels();
		labelList.removeAll(mergeMap.keySet());
		// merge same category
		BugListProcess blp = new BugListProcess(allBugs, labelList);
		blp.mergeCategory(mergeMap);
	}
	public void run(String category){
		ArrayList<ArrayList<BugInfo>> bugSet = ReadExcel.getPosAndNon(allBugs,category);
		BugInfoToTextFile.bugList2FileList(bugSet.get(0),
				"output/"+category+"Bugs/");
		BugInfoToTextFile.bugList2FileList(bugSet.get(1),
				"output/Non"+category+"Bugs/");

		try {
			PrepareTF.run("output/"+category+"Bugs/", "output/Non"+category+"Bugs/",
					"output/LPU/TfFile", "output/LPU/wordList", TfType.TFIDF);
			ArrayList<BugInfo> train = new ArrayList<BugInfo>();
			ArrayList<BugInfo> test = new ArrayList<BugInfo>();
			
			FirstStepForLPU roc = new FirstStepForLPU(MapUtil.getGroundTruth(allBugs));
			roc.getBugs(Constants.LPU_TfFile);
			ArrayList<ArrayList<BugInfo>> posTrainAndTest = BugListProcess
					.divideDataSet(roc.pos, 0.5);
			ArrayList<ArrayList<BugInfo>> negaTrainAndTest = BugListProcess
					.divideDataSet(roc.nonPos, 0.5);
			
			train.addAll(posTrainAndTest.get(0));
			train.addAll(negaTrainAndTest.get(0));
			
			test.addAll(posTrainAndTest.get(1));
			test.addAll(negaTrainAndTest.get(1));
			
			util.WekaDataWrapper.translateToArffTF(train, category,
					"output/LPU/wordList", "output/ACoForest/train.arff");
			util.WekaDataWrapper.translateToArffTF(test, category,
					"output/LPU/wordList", "output/ACoForest/test.arff");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
